package com.dream.springmvc.filter;

import com.dream.springmvc.filter.operators.*;
import com.dream.springmvc.value.*;
import com.dream.springmvc.value.Value;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.ObjectUtils;

import java.util.Objects;

/**
 * @author panwen
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class AmountFilter extends FilterBase<AmountValue> {
    private DoubleOperator op;

    @Override
    protected boolean supports(Value<?> value) {
        return value instanceof AmountValue;
    }

    @Override
    protected boolean doMeet(AmountValue other) {
        // 为空：首先需要单位也为空
        if (DoubleOperator.empty == op && !ObjectUtils.isEmpty(other.getUnit())) {
            return false;
        }
        // 不为空：首先需要单位也不为空
        else if (DoubleOperator.notEmpty == op && ObjectUtils.isEmpty(other.getUnit())) {
            return false;
        }
        // 不等：首先需要单位也不等
        else if (DoubleOperator.ne == op && Objects.equals(getVal().getUnit(), other.getUnit())) {
            return false;
        }
        // 其他：首先需要单位相等
        else if (!Objects.equals(getVal().getUnit(), other.getUnit())){
            return false;
        }
        return op.getOpf().eval(getVal().getValue(), other.getValue());
    }
}
